2020-2021 Sunseeker Telemetry and Lighting System
cs_ex1_DCO1MHzSWTrim.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
60 //******************************************************************************
61 #include "driverlib.h"
62 #include "Board.h"
63 
64 //*****************************************************************************
65 //
66 //Target frequency for MCLK in kHz
67 //
68 //*****************************************************************************
69 #define CS_MCLK_DESIRED_FREQUENCY_IN_KHZ 1000
70 
71 //*****************************************************************************
72 //
73 //MCLK/FLLRef Ratio
74 //
75 //*****************************************************************************
76 #define CS_MCLK_FLLREF_RATIO 30
77 
78 //*****************************************************************************
79 //
80 //Variable to store current Clock values
81 //
82 //*****************************************************************************
83 uint32_t clockValue = 0;
84 
85 //*****************************************************************************
86 //
87 //Variable to store status of Oscillator fault flags
88 //
89 //*****************************************************************************
90 uint16_t status;
91 
92 void main (void)
93 {
94  //Stop watchdog timer
95  WDT_A_hold(WDT_A_BASE);
96 
97  //Set LED2 to output direction
98  GPIO_setAsOutputPin(
99  GPIO_PORT_LED2,
100  GPIO_PIN_LED2
101  );
102 
103  //ACLK set out to pins
104  GPIO_setAsPeripheralModuleFunctionOutputPin(
105  GPIO_PORT_ACLK,
106  GPIO_PIN_ACLK,
107  GPIO_FUNCTION_ACLK
108  );
109  //SMCLK set out to pins
110  GPIO_setAsPeripheralModuleFunctionOutputPin(
111  GPIO_PORT_SMCLK,
112  GPIO_PIN_SMCLK,
113  GPIO_FUNCTION_SMCLK
114  );
115  //MCLK set out to pins
116  GPIO_setAsPeripheralModuleFunctionOutputPin(
117  GPIO_PORT_MCLK,
118  GPIO_PIN_MCLK,
119  GPIO_FUNCTION_MCLK
120  );
121 
122  /*
123  * Disable the GPIO power-on default high-impedance mode to activate
124  * previously configured port settings
125  */
126  PMM_unlockLPM5();
127 
128  //Set DCO FLL reference = REFO
129  CS_initClockSignal(
130  CS_FLLREF,
131  CS_REFOCLK_SELECT,
132  CS_CLOCK_DIVIDER_1
133  );
134 
135  //Set ACLK = REFO
136  CS_initClockSignal(
137  CS_ACLK,
138  CS_REFOCLK_SELECT,
139  CS_CLOCK_DIVIDER_1
140  );
141 
142  //Create struct variable to store proper software trim values
143  CS_initFLLParam param = {0};
144 
145  //Set Ratio/Desired MCLK Frequency, initialize DCO, save trim values
146  CS_initFLLCalculateTrim(
149  &param
150  );
151 
152  //Clear all OSC fault flag
153  CS_clearAllOscFlagsWithTimeout(1000);
154 
155  //For demonstration purpose, change DCO clock freq to 16MHz
156  CS_initFLLSettle(
157  16000,
158  487
159  );
160 
161  //Clear all OSC fault flag
162  CS_clearAllOscFlagsWithTimeout(1000);
163 
164  //Reload DCO trim values that were calculated earlier
165  CS_initFLLLoadTrim(
168  &param
169  );
170 
171  //Clear all OSC fault flag
172  CS_clearAllOscFlagsWithTimeout(1000);
173 
174  //Enable oscillator fault interrupt
175  SFR_enableInterrupt(SFR_OSCILLATOR_FAULT_INTERRUPT);
176 
177  // Enable global interrupt
178  __bis_SR_register(GIE);
179 
180  //Verify if the Clock settings are as expected
181  clockValue = CS_getSMCLK();
182  clockValue = CS_getMCLK();
183  clockValue = CS_getACLK();
184 
185  while (1)
186  {
187  //Toggle LED2
188  GPIO_toggleOutputOnPin(
189  GPIO_PORT_LED2,
190  GPIO_PIN_LED2
191  );
192 
193  //Delay
194  __delay_cycles(1000000);
195  }
196 }
197 
198 
199 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
200 #pragma vector=UNMI_VECTOR
201 __interrupt
202 #elif defined(__GNUC__)
203 __attribute__((interrupt(UNMI_VECTOR)))
204 #endif
205 void NMI_ISR(void)
206 {
207  do {
208  // If it still can't clear the oscillator fault flags after the timeout,
209  // trap and wait here.
210  status = CS_clearAllOscFlagsWithTimeout(1000);
211  } while(status != 0);
212 }
MPU_initThreeSegmentsParam param
#define CS_MCLK_FLLREF_RATIO
void NMI_ISR(void)
uint16_t status
void main(void)
#define CS_MCLK_DESIRED_FREQUENCY_IN_KHZ
uint32_t clockValue
__delay_cycles(500000)